package com.twu.biblioteca.entity;

/**
 * Created with IntelliJ IDEA.
 * User: deva
 * Date: 10/1/13
 * Time: 10:56 AM
 * To change this template use File | Settings | File Templates.
 */
public class Rating {


    public Rating(float value)
    {
        this.value = value;
    }

    @Override
    public String toString() {
        if(value == RATING_NA || value < MIN_RATING || value > MAX_RATING)
            return "N/A";

        return getRatingInStars();
    }

    private String getRatingInStars()
    {
        String ratingAsString = "";
        int flooredRating =(int)Math.floor(value);

        for(int i = 1; i <= flooredRating; i++)
        {
            ratingAsString += RATING_SYMBOL;
            if(i != flooredRating)
                ratingAsString += " ";
        }

        ratingAsString += getHalfRatingStarIfAvailable();

        return ratingAsString;
    }

    private String getHalfRatingStarIfAvailable()
    {
        float decimalValue = value % 1;
        if(decimalValue != 0 && decimalValue >= .5)
        {
                return " " +String.valueOf(HALF_RATING_SYMBOL);
        }
           return"";
    }

    private float value;

    private static final char RATING_SYMBOL = '\u25C9';
    private static final char HALF_RATING_SYMBOL = '\u25D0';

    public static final float RATING_NA = -1;
    private static final int MIN_RATING = 0;
    private static final int MAX_RATING = 10;

}
